<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class Spe extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_spe';




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['status'] = 1;

        $res = $this->insert($data);

        $res = $this->getLastInsID();

        return $res;

    }


    /**
     * @param $data
     * @param $pId
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-10-09 16:48
     */
    public function getTree($data, $pId){
        $tree = array();
        if(!empty($data)){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['cate'] = $this->getTree($data, $v['id']);
                    $tree[]    = $v;
                }
            }
        }
        return $tree;
    }


    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取多规格
     */
    public function goodsSpe($dis){

        $data = $this->where($dis)->select()->toArray();

        return $this->getTree($data,0);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @param $data
     * @param $goods_id
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-10-09 14:45
     */
    public function goodsSpeAdd($data,$goods_id,$uniacid,$i=0){


        $this->dataUpdate(['goods_id'=>$goods_id],['status'=>-1]);

        $isimg_count = 0;

        foreach ($data as $item ){

            if($item['is_img'] == 1){

                $isimg_count++;
            }
        }
        if($isimg_count > 1){

            return ['code'=>400,'msg'=>'当前已有规格名添加了图片，继续添加则需取消上一个规格名的图片勾选'];
        }

        $arr = array();

//        dump($data);exit;
        if(!empty($data)){

            foreach ($data as $v){

                $is_img = $v['is_img']?$v['is_img']:0;

                if(strlen($v['pid'])>10||$i==1){

                    $pid =  $this->dataAdd(['uniacid'=>$uniacid,'goods_id'=>$goods_id,'title'=>$v['title'],'is_img'=>$is_img]);

                }else{

                    $this->dataUpdate(['id'=>$v['id']],['status'=>1,'title'=>$v['title'],'is_img'=>$is_img]);

                    $pid = $v['id'];

                }

                if(!empty($v['cate'])){

                    foreach ($v['cate'] as $value){

                        $image = $is_img?$value['image']:''; #@date 2020/5/14 10:42 --lichuanming

                        if(strlen($value['id'])>10||$i==1) {

                            $id = $this->dataAdd(['uniacid' => $uniacid, 'goods_id' => $goods_id, 'title' => $value['title'], 'pid' => $pid,'is_img'=>$is_img,'image'=>$image]);
                        }else{

                            $this->dataUpdate(['id'=>$value['id']],['status'=>1,'title'=>$value['title'],'is_img'=>$is_img,'image'=>$image]);

                            $id = $value['id'];
                        }

                        $arr[$value['id']] = $id;
                    }
                }
            }
        }
        return $arr;
    }














}